<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
      public function index(){
            //配合武汉app 登录
            $phone=I('Get.phoneNo');
            \Think\Log::record('获取到App程序的加码手机号为' . $phone, 'INFO');
            \Think\Log::record('解密后的手机号为' . appParamBase64Decode($phone), 'INFO');
            if($phone!="" ||$phone!=null || !empty($phone)){
                  $User = M('user','e_','ecrm');
                  $where['user_cell'] =appParamBase64Decode($phone);
                  // 把查询条件传入查询方法
                  $data= $User->where($where)->find();
                  if($data!=null){
                        //判断是否记录过该用户
                        $ModAct = M('user','ny_','newyear');
                        $whereAct['id'] = $data['user_id'];
                        $Act= $ModAct->where($whereAct)->find();
                        if($Act==null || $Act==""){
                              //记录 用户 信息
                              $actUser['id'] = $data['user_id'];
                              $actUser['email'] = $data['email'];
                              $actUser['user_name'] = $data['nick_name'];
                              $actUser['user_turename'] = $data['real_name'];
                              $actUser['phone'] = $data['user_cell'];
                              $actUser['head_img']=$data['user_image'];
                              $actUser['addtime'] =time();
                              $ModAct->add($actUser);
                        }
                        //记录下登陆日志
                        log_login($data['user_id']);
                        \Think\Log::record('App登录成功！！！', 'INFO');
//                   记录登录信息到Cookie
                        cookie('uid',$data['user_id'],3600); // 指定cookie保存时间
                        cookie('nick_name',$data['nick_name'],3600); // 指定cookie保存时间
                        cookie('email',$data['email'],3600); // 指定cookie保存时间
                        cookie('user_cell',$data['user_cell'],3600); // 指定cookie保存时间
                        cookie('user_truename',$data['real_name'],3600); // 指定cookie保存时间
                  }
            }
            //生成token
            $token=creatToken();
            //检查来访的是pc还是手机端
            if(isMobile()){
                  $uid = cookie('uid');
                  $nick_name = cookie('nick_name');
                  $login="";
                  $login2="<li><a href=\"index.php?m=Home&c=mobile&a=jumpToHtml&op=login\">登录</a></li>";
                  if($uid!=null){
                        $User_Score=GetUserSocreByUid($uid);
                        $login="<span>欢迎".$nick_name."</span><span id='score'>(积分:".(int)$User_Score.")</span><a href=\"index.php?m=Home&c=Mobile&a=Loginout\">退出</a>";
                        $login2="";
                  }
                  $this->assign('loginString',$login);// 赋值分页输出
                  $this->assign('login',$login2);// 赋值分页输出

                  $this->display("mobile:index");
                          }
            else{
                    //构建签到页面

                  $NowDate=strtotime(date('Y-m-d H:i:s'));
                  $Start3=strtotime('2017-1-11 00:00:00');
                  $End3=strtotime('2017-1-25 23:59:59');
                  $Start4=strtotime('2017-1-26 00:00:00');
                  $End4=strtotime('2017-2-10 23:59:59');
                  $Start5=strtotime('2017-2-11 00:00:00');
                  $End5=strtotime('2018-2-19 23:59:59');
                  if($NowDate>=$Start3 && $NowDate<=$End3){
                        $SignStr="
						<li class=\"pic02\" id=\"qd1\">

						</li>
						<li class=\"pic03\" id=\"qd2\">

						</li>
                                     <li class=\"pic01\" id=\"qd3\">

						</li>
						<li class=\"pic04\" id=\"qd4\">

						</li>
						<li class=\"pic05\" id=\"qd5\">

						</li>";
                  }
                  if($NowDate>=$Start4 && $NowDate<=$End4){
                        $SignStr="
	                             <li class=\"pic03\" id=\"qd2\">

						</li>
	                              <li class=\"pic01\" id=\"qd3\">

						</li>

						<li class=\"pic04\" id=\"qd4\">

						</li>

						<li class=\"pic05\" id=\"qd5\">

						</li>
						<li class=\"pic02\" id=\"qd1\">

						</li>";
                  }
                  if($NowDate>=$Start5 && $NowDate<=$End5){
                        $SignStr="
	                            <li class=\"pic01\" id=\"qd3\">

						</li>
						<li class=\"pic04\" id=\"qd4\">

						</li>

						<li class=\"pic05\" id=\"qd5\">

						</li>

						<li class=\"pic02\" id=\"qd1\">

						</li>

						<li class=\"pic03\" id=\"qd2\">

						</li>";
                  }

//end
                  $this->assign('SignStr',$SignStr);// 赋值输出
//                  $this->assign('span1',$span1);// 赋值输出
//                  $this->assign('span2',$span2);// 赋值输出
//                  $this->assign('span3',$span3);// 赋值输出
//                  $this->assign('span4',$span4);// 赋值输出
//                  $this->assign('span5',$span5);// 赋值输出
                  $this-> loginStr();
               }
      }

      public  function  ShowIsSign(){
            $span1="";
            for($i = strtotime('2016-12-24'); $i <= strtotime('2016-12-29'); $i += 86400) {
                  $qd=GetQdBytime($i);
                  if($qd!=null){
                        $span1.="<span id='$i' class=\"current\"></span>";
                  }else{
                        $span1.="<span id='$i'></span>";
                  }
            }
            $span2="";
            for($i = strtotime('2016-12-30'); $i <= strtotime('2017-1-10'); $i += 86400) {
                  $qd=GetQdBytime($i);
                  if($qd!=null){
                        $span2.="<span id='$i' class=\"current\"></span>";
                  }else{
                        $span2.="<span id='$i'></span>";
                  }
            }

            $span3="";
            for($i = strtotime('2017-1-11'); $i <= strtotime('2017-1-25'); $i += 86400) {
                  $qd=GetQdBytime($i);
                  if($qd!=null){
                        $span3.="<span id='$i' class=\"current\"></span>";
                  }else{
                        $span3.="<span id='$i'></span>";
                  }
            }

            $span4="";
            for($i = strtotime('2017-1-26'); $i <= strtotime('2017-2-10'); $i += 86400) {
                  $qd=GetQdBytime($i);
                  if($qd!=null){
                        $span4.="<span id='$i' class=\"current\"></span>";
                  }else{
                        $span4.="<span id='$i'></span>";
                  }
            }

            $span5="";
            for($i = strtotime('2017-2-11'); $i <= strtotime('2017-2-19'); $i += 86400) {
                  $qd=GetQdBytime($i);
                  if($qd!=null){
                        $span5.="<span id='$i' class=\"current\"></span>";
                  }else{
                        $span5.="<span id='$i'></span>";
                  }
            }

            $data['span1']=$span1;
            $data['span2']=$span2;
            $data['span3']=$span3;
            $data['span4']=$span4;
            $data['span5']=$span5;
            $this->ajaxReturn($data);// 赋值分页输出
      }
      //
    public  function  showExchangePrize(){
          $uid = cookie('uid');
          $nick_name = cookie('nick_name');

          if($uid!=null){
                $score=GetUserSocreByUid(cookie("uid"));
                $loginStr="<li class=\"fr\"><a  href=\"javascript:;\">".$nick_name."(积分:<span id='score'>".$score."</span>)</a></li><li class=\"fr\"><a href=\"index.php?m=Home&c=index&a=Loginout\">退出</a></li>";
          }else{
                $loginStr="<li class=\"fr\"><a href=\"javascript:;\" onclick=\"sho()\">登录</a></li><li class=\"fr\"><a  href=\"http://www.mypeugeot.com.cn/?register_source=2017\">会员注册</a></li>";
          }
          $this->assign('loginStr',$loginStr);// 赋值分页输出
          $this->display("change"); // 输出模板
    }
      //展示 所有 兑换奖品
      public  function  showAllPrize(){
            $list=  get_All_DuihuanPirzeInfo();
            $this->ajaxReturn($list,'JSON');
      }

      /**
       * 签到
       */
      public  function  Sign(){
            if(strtotime(date('Y-m-d H:i:s'))>=strtotime('2017-2-19 12:59:59')){
                  $msg['status']  = 503;
                  $msg['content'] = '活动已经结束哦！';
                  $this->ajaxReturn($msg);
                  exit();
            }
            $id=cookie("uid");
            if($id==""){
                  $ajaxReturn['content']='您尚未登录!';
                  $this->ajaxReturn($ajaxReturn);
            }
            //判断今天是否签到过
            $Sign=D('Sign');
            $where['create_time']=strtotime(date("Y-m-d"));
            $where['uid']=$id;
            $CkTodayHaveSign=$Sign->where($where)->find();
            if($CkTodayHaveSign || count($CkTodayHaveSign)>0){
                  \Think\Log::write('今天已经签到过 签到时间为：'.date("Y-m-d"),'INFO');
                  $ajaxReturn['nowDate']=strtotime(date("Y-m-d"));
                  $ajaxReturn['score']=0;
                  $ajaxReturn['content']='您今天已经签到过，明天再来吧!';
                  $this->ajaxReturn($ajaxReturn);
                  exit;
            }
            unset($st);
            unset($Sign);
            //检查是否是连续签到  只要判断最后签到的天数 和现在的时间相差>1就说明不是连续签到
            $Sign=D('Sign');
            $where2['uid']=$id;
            $CkContinueSign=$Sign->where($where2)->order('create_time desc')->select();
            unset($Sign);
            if($CkContinueSign ||count($CkContinueSign)>0){ //检查是否连续签到
                  $time= $CkContinueSign[0]['create_time'];
                  \Think\Log::write('最后一次签到的时间 签到时间为：'.date("Y-m-d",$time),'INFO');
//                  echo "最后一次签到的时间".date("Y-m-d",$time);
                  $NowTime= strtotime(date("Y-m-d"));
//                  $diff=  $NowTime-$time;
                  $diff=  diffBetweenTwoDays($time,$NowTime);
                  \Think\Log::write("最后签到的时间和今天相差天数为".$diff.'INFO');
                  if($diff==1){
//                       echo "持续签到加积分操作";
                        //新增一条 签到记录
                        writeSign($id,null);
                        //获取持续签到的天数
                        $Sign=D('Sign');
                        $where3['uid']=$id;
                        $where3['isbreak']=1;
                        $CkContinueSign2=$Sign->where($where3)->order('create_time desc')->find();
                        if($CkContinueSign2 || count($CkContinueSign2)>0){
                              $time2= $CkContinueSign2['create_time'];
                              \Think\Log::write("最后一次断签时间".date("Y-m-d",$time2),'INFO');
                              \Think\Log::write("当前时间".date("Y-m-d",$NowTime).'INFO');
//                              $diff23= $NowTime-$time2;
                              $diff24= diffBetweenTwoDays($time2,$NowTime);
                              \Think\Log::write("断签的天数和现在的时间相差".$diff24."天",'INFO');
                              Add_user_points($id,$diff24+1,1,155);
//                               echo "您今天已经签到过，明天再来吧";
                              \Think\Log::write('签到时间为：'.date("Y-m-d",$time),'INFO');
                              $ajaxReturn['score']=$diff24+1;
                              $ajaxReturn['nowDate']=strtotime(date("Y-m-d"));
                              $ajaxReturn['content']=($diff24+1).'积分入囊中<br/>随意花';
                              $this->ajaxReturn($ajaxReturn);
                        }else{
                              Add_user_points($id,count($CkContinueSign)+1,1,155);
//                               echo "您今天已经签到过，明天再来吧";
                              $ajaxReturn['score']=count($CkContinueSign)+1;
                              $ajaxReturn['nowDate']=strtotime(date("Y-m-d"));
                              $ajaxReturn['content']=(count($CkContinueSign)+1).'积分入囊中<br/>随意花!';
                              $this->ajaxReturn($ajaxReturn);
                        }
                  }else{
//                       echo "断签签到加积分操作";
                        //新增一条 签到记录
                        writeSign($id,1);
                        Add_user_points($id,1,1,155);
//                         echo "您今天已经签到过，明天再来吧";
                        $ajaxReturn['score']=1;
                        $ajaxReturn['nowDate']=strtotime(date("Y-m-d"));
                        $ajaxReturn['content']='1积分入囊中<br/>随意花!';
                        $this->ajaxReturn($ajaxReturn);
                  }
            }else{//第一次签到
//                       echo "我是第一次签到";
                  \Think\Log::write($id.'我是第一次签到：'.date("Y-m-d"),'INFO');
                  //新增一条 签到记录
                  Add_user_points($id,1,1,155);
                  writeSign($id,null);
                  $ajaxReturn['score']=1;
                  $ajaxReturn['nowDate']=strtotime(date("Y-m-d"));
                  $ajaxReturn['content']="1积分入囊中<br/>随意花!";
                  $this->ajaxReturn($ajaxReturn);
            }
      }


      /**
       * 验证码接口
       */
      public function yzmApi(){
            $config = array(
                  'fontSize'    =>    22,    // 验证码字体大小
                  'length'      =>    4,     // 验证码位数
                  'useNoise'    =>    true, // 验证码杂点
                  'imageW'      =>   150 ,
                  'imageH'      =>   50,
                  'codeSet'     => "1234567890",
                  'reset'     =>  true,
                  'fontttf '=> '5.ttf'
            );
            $Verify = new \Think\Verify($config);
            $Verify->entry();
      }


      /**
       * 登录api
       */
      public function loginApi(){
            //获取用户参数
            $phone= I('post.phone');
            $Pwd= I('post.pwd');
            $code=  I('post.code');
            \Think\Log::write('登录开始：$phone='.$phone,'INFO');
            //验证验证码
            $verify = new \Think\Verify();
            if(!$verify->check($code)){
                  $msg['status']  = 500;
                  $msg['content'] = '验证码错误';
                  $this->ajaxReturn($msg);
                  exit();
            }

            if(preg_match("/1[345678]{1}\d{9}$/",$phone)){
                  $data= Get_user_by_condition($phone);
            }else{
                  $data= Get_user_by_email($phone);
            }
            if($data==null || $data==""){
                  $msg['status']  = 400;
                  $msg['content'] = '帐号不存在';
                  $this->ajaxReturn($msg);
                  exit();
            }
              $pwd=md5($Pwd); //验证密码
            if($data['user_pwd']!=$pwd){
                  $msg['status']  = 401;
                  $msg['content'] = '密码不正确';
                  $this->ajaxReturn($msg);
                  exit();
            }

            $logM = M('log','ny_login_','newyear');
            $whereLog['strip'] = get_client_ip();
            $map['addtime'] = array('gt',strtotime(date("Y-m-d")));
            $logMs = $logM->where($map)->where($whereLog)->group("uid")->select();
            if(count($logMs) > 5 && $whereLog['strip']!='124.205.93.23'){
                  if($whereLog['strip']=='124.205.63.253'){
                        \Think\Log::write('登录-验证IP：IP='.$whereLog['strip']."-------logMs=".count($logMs),'INFO');
                  } else {
                        \Think\Log::write('登录-验证IP：IP='.$whereLog['strip']."-------logMs=".count($logMs),'INFO');
                        $msg['status']  = 5002;
                        $msg['content'] = '请勿重复操作！';
                        $this->ajaxReturn($msg);
                        exit();
                  }
            }

            \Think\Log::write('登录-验证通过：$phone='.$phone,'INFO');
            //判断是否记录过该用户
            $ModAct = M('user','ny_','newyear');
            $whereAct['id'] = $data['user_id'];
            $Act= $ModAct->where($whereAct)->find();
            if($Act==null || $Act==""){
                  //记录 用户 信息
                  $actUser['id'] = $data['user_id'];
                  $actUser['email'] = $data['email'];
                  $actUser['user_name'] = $data['nick_name'];
                  $actUser['user_turename'] = $data['real_name'];
                  $actUser['phone'] = $data['user_cell'];
                  $actUser['head_img']=$data['user_image'];
                  $actUser['addtime'] =time();
                  $ModAct->add($actUser);
            }
            //记录下登陆日志
            log_login($data['user_id']);
            \Think\Log::write('登录成功：$phone='.$phone,'INFO');
            cookie('uid',$data['user_id'],3600); // 指定cookie保存时间
            cookie('nick_name',$data['nick_name'],3600); // 指定cookie保存时间
            cookie('email',$data['email'],3600); // 指定cookie保存时间
            cookie('user_cell',$data['user_cell'],3600); // 指定cookie保存时间
            cookie('user_truename',$data['real_name'],3600); // 指定cookie保存时间
            $msg['status']  = 200;
            $msg['content'] = '登录成功';
            $this->ajaxReturn($msg);
            exit();
      }
      //退出登录
      public function Loginout(){
            cookie('uid',null); // 指定cookie保存时间
            cookie('nick_name',null); // 指定cookie保存时间
            cookie('email',null); // 指定cookie保存时间
            cookie('user_cell',null); // 指定cookie保存时间
            cookie('user_truename',null); // 指定cookie保存时间
           $this-> loginStr();
      }

    private  function loginStr(){
            $uid = cookie('uid');
            $nick_name = cookie('nick_name');
            if($uid!=null){
                  $score=GetUserSocreByUid(cookie("uid"));
                  $loginStr="<li class=\"fr in\"><a  href=\"javascript:;\">".$nick_name."(积分:<span id='score'>".$score."</span>)</a></li><li class=\"fr in\"><a href=\"index.php?m=Home&c=index&a=Loginout\">退出</a></li>";
            }else{
                  $loginStr="<li class=\"fr in\"><a href=\"javascript:;\" onclick=\"sho()\">登录</a></li>	<li class=\"fr\">
						<a href=\"http://www.mypeugeot.com.cn/?register_source=2017\">会员注册</a>
					</li>";
            }
            $this->assign('loginStr',$loginStr);// 赋值分页输出
            $this->display("index"); // 输出模板
      }
      /**
       * 积分兑换 api
       */
      public  function   convert(){
            $pid= I("post.pid");
            $token=I("post.cnm");
            \Think\Log::write('传入的$token'.$token,'INFO');
            \Think\Log::write('session的$token'.session('TOKEN'),'INFO');
            if (!checkToken($token)) {
                  //重新生成token
                  $ResetToken= creatToken();
                  $msg['cnm'] = $ResetToken;
                  $msg['msg'] = '请勿重复操作！';
                  \Think\Log::write('$token验证手机号'.cookie("user_cell"),'INFO');
                  $this->ajaxReturn($msg);
                  exit();
            }
            if(strtotime(date('Y-m-d H:i:s'))>=strtotime('2017-2-19 12:59:59')){
                  //重新生成token
                  $ResetToken= creatToken();
                  $msg['cnm'] = $ResetToken;
                  $msg['status']  = 503;
                  $msg['msg'] = '活动已经结束哦！';
                  $this->ajaxReturn($msg);
                  exit();
            }
            if(checkIsLogin()){
                  //重新生成token
                  $ResetToken= creatToken();
                  $msg['cnm'] = $ResetToken;
                  $msg['status']  = 504;
                  $msg['msg'] = '您尚未登陆！';
                  $this->ajaxReturn($msg);
                  exit();
             }
            $prize= GetDuiInfoByPid($pid);

            //检查今天的该奖品发放个数   1 为兑换的类型  2 为抽奖的类型
            $result=GetTodayPrizeHave($pid,1);

            if(count($result)>=$prize['numperday']){
                  //重新生成token
                  $ResetToken= creatToken();
                  $msg['cnm'] = $ResetToken;
                  $msg['status']  = 507;
                  $msg['msg'] = '该奖品今天已经兑换完，请明天再来兑换,每日12点整刷新奖品！';
                  $this->ajaxReturn($msg);
                  exit();
            }

            if($prize['left']<=0){
                  //重新生成token
                  $ResetToken= creatToken();
                  $msg['cnm'] = $ResetToken;
                  $msg['status']  = 505;
                  $msg['msg'] = '该奖品已经兑换完';
                  $this->ajaxReturn($msg);
                  exit();
            }

            // 判断 是否足够 积分 来兑奖
            $user_point=GetUserSocreByUid(cookie("uid"));
            if($user_point<$prize['score']){
                  //重新生成token
                  $ResetToken= creatToken();
                  $msg['cnm'] = $ResetToken;
                  $msg['status']  = 508;
                  $msg['msg'] = '您的积分不足兑换该奖品';
                  $this->ajaxReturn($msg);
                  exit();
            }



            if(CheckIsDuiByPid($pid,1)){
                  //重新生成token
                  $ResetToken= creatToken();
                  $msg['cnm'] = $ResetToken;
                  $msg['status']  = 510;
                  $msg['msg'] = '您已经兑换过该奖品,不可重复兑换！';
                  $this->ajaxReturn($msg);
                  exit();
            }
//                  //记录中奖记录
//                   log_prize($prize['id']);
            //判断是否是卡券类 兑换码 的奖品
            if($prize['is_code_prize']==1){
                  //根据pid 去兑换码库里找一个码
                  $code=GetCodeByPid();
                  if($code==null || $code==""){
                        //重新生成token
                        $ResetToken= creatToken();
                        $msg['cnm'] = $ResetToken;
                        $msg['status']  = 408;
                        $msg['msg'] = '没有可用的爱奇艺兑换码';
                        $this->ajaxReturn($msg);
                        exit();
                  }

            }
            if($prize['is_url_prize']==1){//判断是否是url类的兑换奖品
                  //根据pid 去兑换码库里找一个url码
                  $url_code=GetCodeUrlByPid();
                  if($url_code==null || $url_code==""){
                        //重新生成token
                        $ResetToken= creatToken();
                        $msg['cnm'] = $ResetToken;
                        $msg['status']  = 408;
                        $msg['msg'] = '没有可用的星巴克兑换码';
                        $this->ajaxReturn($msg);
                        exit();
                  }
            }
            //判断是否是现金兑换券
            //获取所有的现金数值
            $money = "30,50,100,150,200,300,350,400,450,500,550,600";
            $money_arr = explode(',',$money);
            if(in_array($prize['is_money_prize'],$money_arr)){
                  $money_code = GetMoneyCodeByNum($prize['is_money_prize']);
                  if($money_code==null || $money_code==""){
                        //重新生成token
                        $ResetToken= creatToken();
                        $msg['cnm'] = $ResetToken;
                        $msg['status']  = 408;
                        $msg['msg'] = '没有可用的现金兑换券';
                        $this->ajaxReturn($msg);
                        exit();
                  }
               }

            //减少奖品数量1
            $checkIsOk=  pushNumByPid($prize['id'],$prize['left']);
            if($checkIsOk==0 || $checkIsOk==false){
                  //重新生成token
                  $ResetToken= creatToken();
                  $msg['cnm'] = $ResetToken;
                  $msg['status']  = 511;
                  $msg['msg'] = '发生错误 错误代码511！请联系狮友汇客服告知错误代码进行解决！';
                  $this->ajaxReturn($msg);
                  exit();
            }
            if($checkIsOk==1){//扣减奖品数量成功 将$code 或者 $url_code 状态改为 1

                  if($code['prize_code']!=null || $code['prize_code']!=""){
                        changeStatusByCode($code['id']);
                  }
                  if($url_code['prize_url']!=null || $url_code['prize_url']!=""){
                        changeStatusByurl_code($url_code['id']);
                  }
                  if($money_code['money_code']!=null || $money_code['money_code']!=""){
                        changeStatusBymoney_code($money_code['id']);
                  }
            }

            //扣除用户的积分
            $QC_res =Deduction_User_points($prize['score']);
            if($QC_res==0 || $QC_res==false){
                  //重新生成token
                  $ResetToken= creatToken();
                  $msg['cnm'] = $ResetToken;
                  $msg['status']  = 566;
                  $msg['msg'] = '发生错误 错误代码566！请联系狮友汇客服告知错误代码进行解决！';
                  $this->ajaxReturn($msg);
                  exit();
            }
            //记录 积分项目 到ecrm 的积分项目
            Add_Log_ecrm_pointDetil($prize['item_id'],$prize['score']);
            //记录中奖用户  1 为兑换的类型  2 为抽奖的类型
            Add_log_user_prize($prize['id'],1,$code['prize_code'],$url_code['prize_url'],$money_code['money_code']);
            //重新生成token
            $ResetToken= creatToken();
            $msg['cnm'] = $ResetToken;
            $msg['status']  = 200;
            $msg['msg'] = '您已成功兑换<br><span>'.$prize['pname'].'</span>';
            $this->ajaxReturn($msg);
            exit();
      }

      /*
       * 保存用户信息地址
       */
      public  function  Save_User_Address(){
            $phone=I('post.phone');
            $user_turename=I('post.user_turename');
            $address=I('post.address');
            Save_Address($phone,$user_turename,$address);
      }

      /**
       * 获取经销商城市信息
       */
      public function getDealerCity(){
            $dealerPrivince=get_dealer_privince();
            $privince = array();
            foreach($dealerPrivince as $privinceVal){
                  $dealerCity=get_dealer_city_from_privince($privinceVal['privince']);
                  $city = array();
                  foreach($dealerCity as $cityVal){
                        array_push($city,$cityVal['city']);
                  }
                  $privince[$privinceVal['privince']]=$city;
            }
            $this->ajaxReturn($privince,'JSON');
      }

      /**
       * 获取城市中的经销商
       */
      public function getDealerInfo(){
            $city= I('post.city');
            $dealerInfo=get_dealer_info($city);
            $dealer = array();
            foreach($dealerInfo as $val){
                  array_push($dealer,$val);
            }
            $this->ajaxReturn($dealer,'JSON');
      }

      /**
       * 获取用户的礼品
       */
      public function getUserActPrize(){
            if(checkIsLogin()){
                  $msg['status']  = 504;
                  $msg['msg'] = '您尚未登陆！';
                  $this->ajaxReturn($msg);
                  exit();
            }
            $actPrize = get_user_act_prize();

            $prizes = array();
            $exchangem= M('exchange','ny_prize_','newyear');
            $lottom= M('lotto','ny_prize_','newyear');
            foreach($actPrize['prizes'] as $prize){
                  $prizeWhere['id'] = $prize['pid'];
                  if($prize['type']==1){//兑奖
                        $exchange=$exchangem->where($prizeWhere)->select();
                        $prize['pname']=$exchange[0]['pname'];
                        $prize['explain']=$exchange[0]['explain'];
                        $prize['type']="兑奖";
                  }
                  if($prize['type']==2){//抽奖
                        $lotto=$lottom->where($prizeWhere)->select();
                        $prize['pname']=$lotto[0]['pname'];
                        $prize['explain']=$lotto[0]['explain'];
                        $prize['type']="抽奖";
                  }
                  if($prize['code_url']==null){
                        $prize['code_url']="———";
                  }
                  if($prize['code']==null){
                        $prize['code']=$prize['code_url'];
                  }
                  $prize['subtime'] = date("Y-m-d", $prize['subtime']);
                  array_push($prizes,$prize);
            }

            $user= D('User');
            $userWhere['id'] = cookie('uid');
            $userinfo = $user->where($userWhere)->select();

            $actPrize['prizes'] = $prizes;
            $actPrize['user'] = $userinfo;
            $actPrize['status'] = 200;
            $this->ajaxReturn($actPrize,'JSON');
      }

      /**
       * 获取获奖名单
       */
      public function getAllActPrize(){
            $actPrize = get_all_act_prize();

            $prizes = array();
            $user= D('User');
//            $exchangem= M('exchange','ny_prize_','newyear');
            $lottom= M('lotto','ny_prize_','newyear');
            foreach($actPrize['prizes'] as $prize){
                  $userWhere['id'] = $prize['uid'];
                  $userinfo = $user->where($userWhere)->select();
                  $prize['username']=$userinfo[0]['user_name'];
                  $prizeWhere['id'] = $prize['pid'];
//                  if($prize['type']==1){//兑奖
//                        $prize['type']="兑奖";
//                        $exchange=$exchangem->where($prizeWhere)->select();
//                        $prize['pname']=$exchange[0]['pname'];
//                  }
                  if($prize['type']==2){//抽奖
                        $lotto=$lottom->where($prizeWhere)->select();
                        $prize['pname']=$lotto[0]['pname'];
                        $prize['type']="抽奖";
                  }
                  $prize['subtime'] = date("Y-m-d", $prize['subtime']);
                  array_push($prizes,$prize);
            }
            $actPrize['prizes'] = $prizes;
            $actPrize['status'] = 200;
            $this->ajaxReturn($actPrize,'JSON');
      }


}